﻿/**
* jQuery Twi.Confirm plugin
* @author: feitianxinhong  http://xuzhihong1987.blog.163.com/
* @version 1.0.0
* @create date:2014-06-19
* @copyright (c) 2014 feitianxinhong/tyingsoft/twilight  http://www.tyingsoft.com
* @example Visit http://blog.163.com/twi_fly
*/
(function () {
	var global = this;
	if (typeof Twi === 'undefined') {
		global.Twi = {};
	}
	Twi.global = global;
	Twi.Confirm = function (config) {
		config = $.extend({
			html: '确定要执行此操作?',
			contentEl: null,
			showCancel: true,
			btnOKText: '确定',
			btnCancelText: '取消',
			appendTo: 'body',
			fn: function () { }
		}, config);
		this.config = config;
		this.id = "TwiConfirm" + Math.floor(100000 * Math.random()); //随机ID     
	}
	Twi.Confirm.prototype.show = function () {
		var html = '<div class="twiConfirmContainer" id="' + this.id + '">';
		html += '<div class="twiConfirm">';
		html += '<div class="twiConfirmBody">'+this.config.html+'</div>';
		html += '<div class="twiConfirmFooter">';
		html += '<a class="twiConfirmBtn twiConfirmBtnOK">' + this.config.btnOKText + '</a>';
		if (this.config.showCancel) {
			html += '<a class="twiConfirmBtn twiConfirmBtnCancel">' + this.config.btnCancelText + '</a>';
		}
		html += '</div>';
		html += '</div>';
		html += '<div class="twiConfirmBackdrop"></div>';
		html += '</div>';
		$(this.config.appendTo).append(html);

        //加载现有HTML为内容
		if (this.config.contentEl && $(this.config.contentEl).length > 0) {
		    $('#' + this.id).find('.twiConfirmBody').empty().append($(this.config.contentEl).html());
		}

        //取消事件
		if (this.config.showCancel) {
		    var self = this;
		    $('#' + this.id).find('.twiConfirmBtnCancel').click(function () {
		        self.close();
		    });
		}

        //确定事件
		if (typeof this.config.fn == "function") {
		    var self = this;
		    $('#' + this.id).find('.twiConfirmBtnOK').click(function () {
		        self.config.fn();
		        self.close();
		    });
		}
	}
	Twi.Confirm.prototype.close = function () {
		$('#' + this.id).remove();
	}
})();

